/*******************************************************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Nicolas DAURES
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *******************************************************************************************************************/


/****************************************************************
 * Constants
 ****************************************************************/

var FPS = 60;
var FPS_MARGIN = [100, 10];

var CANVAS_SIZE = [640, 480];
var CANVAS_HALFSIZE = [320, 240];


/****************************************************************
 * Game launcher
 ****************************************************************/

window.addEventListener('load',
			function ()
			{
			    // Get the canvas
			    var elem = document.getElementById('canvas');
			    if (!elem || !elem.getContext)
			    {
					return;
			    }
			    
			    // Get the 2D context
			    context = elem.getContext('2d');
			    if (!context)
			    {
					return;
			    }
			    
				// Keyboard event listener
			    window.document.onkeydown = onKeyDown;
			    window.document.onkeyup = onKeyUp;
			    
				// Mouse event listener
			    window.document.onmousedown = onMouseDown;
			    window.document.onmouseup = onMouseUp;
				window.document.onmousemove = onMouseMove;
				
			    // Initialize the test
			    initializeTest();
			    
			    // Game loop
			    iGameLoopId = setInterval(updateTest, 1000/FPS);
			},
			false);

var textKey = null;
var textMouseButton = null;
var textMousePosition = null;

function initializeTest()
{
    // Initialize engines
    SceneEngine.getInstance();
	TimeEngine.getInstance();
	RenderEngine.getInstance();
	CameraEngine.getInstance();
	InputEngine.getInstance();
	
	// Debug infos
	TimeEngine.getInstance().displayFPS(new b2Vec2(240, -230));

	var textKeyPos = new b2Vec2(-300, 200);
	textKey = new Text2D();
	textKey.setColor("#000000");
    textKey.setFont("22px arial");
    textKey.setText("Key : XXX, State : XXX");
	textKey.getSceneNode().setPosition(textKeyPos);
	SceneEngine.getInstance().getRootSceneNode().attachSceneNode(textKey.getSceneNode());
	RenderEngine.getInstance().addRenderable(textKey);
	
	var textMouseButtonPos = new b2Vec2(-300, 100);
	textMouseButton = new Text2D();
	textMouseButton.setColor("#000000");
    textMouseButton.setFont("22px arial");
    textMouseButton.setText("Mouse : XXX, State : XXX");
	textMouseButton.getSceneNode().setPosition(textMouseButtonPos);
	SceneEngine.getInstance().getRootSceneNode().attachSceneNode(textMouseButton.getSceneNode());
	RenderEngine.getInstance().addRenderable(textMouseButton);
	
	var textMousePositionPos = new b2Vec2(-300, 0);
	textMousePosition = new Text2D();
	textMousePosition.setColor("#000000");
    textMousePosition.setFont("22px arial");
    textMousePosition.setText("Mouse Pos : XXX, XXX");
	textMousePosition.getSceneNode().setPosition(textMousePositionPos);
	SceneEngine.getInstance().getRootSceneNode().attachSceneNode(textMousePosition.getSceneNode());
	RenderEngine.getInstance().addRenderable(textMousePosition);
}

function updateTest()
{
    // Update engines
    TimeEngine.getInstance().update();
	SceneEngine.getInstance().update();
	RenderEngine.getInstance().update();
	CameraEngine.getInstance().update();
	InputEngine.getInstance().update();
	
	// Get first key pressed
	var keyState = Keyboard.getInstance().m_aKeys[0];
	for (var keyIndex = 0; keyIndex < Keyboard.getInstance().m_aKeys.length; keyIndex++)
	{
		keyState = Keyboard.getInstance().m_aKeys[keyIndex];
		if (keyState != InputState.NONE)
		{
			break;
		}
	}
	textKey.setText("Key : " + keyIndex + ", State : " + keyState);

	// Get first mouse button pressed
	var mouseState = Mouse.getInstance().m_aButtons[0];
	for (var mouseButtonIndex = 0; mouseButtonIndex < Mouse.getInstance().m_aButtons.length; mouseButtonIndex++)
	{
		mouseState = Mouse.getInstance().m_aButtons[mouseButtonIndex];
		if (mouseState != InputState.NONE)
		{
			break;
		}
	}
	textMouseButton.setText("Mouse : " + mouseButtonIndex + ", State : " + mouseState);
	
	// Get the mouse position
	textMousePosition.setText("Mouse Pos : " + Mouse.getInstance().m_v2Pos.x + ", " + Mouse.getInstance().m_v2Pos.y);
}
